﻿
Imports std
Imports xmlctrl

Public Module utils
    Public Const csscommentBegin As String = "/*"
    Public Const csscommentEnd As String = "*/"
    Public ReadOnly crunchReplacer As vector(Of pair(Of String, String)) = Nothing

    Private Sub kickblank(ByVal c As Char)
        crunchReplacer.push_back(make_pair(Of String, String)(c + Global.character.blank, c))
        crunchReplacer.push_back(make_pair(Of String, String)(Global.character.blank + c, c))
    End Sub

    Sub New()
        'what needs to be done here,
        ' 1. remove all \r\n \r \n
        ' 2. remove all blanks before and after :/;/{/}/' '<blank>/\t
        crunchReplacer = New vector(Of pair(Of String, String))()
        crunchReplacer.push_back(make_pair(newline.incode(), emptyString))
        crunchReplacer.push_back(make_pair(Of String, String) _
                                          (Global.character.newline, emptyString))
        crunchReplacer.push_back(make_pair(Of String, String) _
                                          (Global.character.return, emptyString))
        crunchReplacer.push_back(make_pair(Of String, String) _
                                          (Global.character.tab, Global.character.blank))
        crunchReplacer.push_back(make_pair(Of String, String) _
                                          (Global.character.blank + Global.character.blank, Global.character.blank))
        kickblank(Global.character.colon)
        kickblank(Global.character.semicolon)
        kickblank(Global.character.leftBrace)
        kickblank(Global.character.rightBrace)
    End Sub

    Public Sub csscrunch(ByRef content As String)
        kickBetween(content, csscommentBegin, csscommentEnd)
        Dim temp As String = Nothing
        Do
            copy(temp, content)
            strrplc2(content, crunchReplacer, False)
        Loop Until strsame(temp, content, False)
    End Sub

    Public Function truncate(ByVal s As String, ByVal maxwidth As Int64) As String
        Dim len As Int64
        len = strlen(s)
        If len > (maxwidth / maxcharWidth) Then
            assert(strlen(xmlcontrol.tagleading()) = 1, _
                   "strlen(xmlcontrol.tagleading()) > 1, will break the logic of truncate. need update.")
            Static ellipsisString As String = Global.character.blank + ellipsis
            Dim i As Int64
            Dim width As Int64 = 0
            Dim intag As Boolean = False
            maxwidth -= stringwidth(ellipsisString)
            For i = 0 To len - 1
                If s(i) = xmlcontrol.tagleading() Then
                    debugassert(Not intag, "xml output has tag overlapping. s = " + s)
                    intag = True
                ElseIf s(i) = xmlcontrol.tagfinal() Then
                    debugassert(intag, "xml output has a tagfinal not paired with a tagleading. s = " + s)
                    intag = False
                ElseIf Not intag Then
                    width += charwidth(s(i))
                    If width > maxwidth Then
                        Exit For
                    End If
                End If
            Next
            If i < len Then
                Return strleft(s, i) + ellipsisString
            Else
                Return s
            End If
        Else
            Return s
        End If

#If RETIRED Then
            ' old logic, but will decrease the length of output
            Dim index As Int64
            index = ds.maxlength - strlen(ellipsis) - 1
            Dim laststart As Int64
            laststart = strlastindexof(s, xmlcontrol.tagleading(), index, 1)
            Dim lastend As Int64
            lastend = strlastindexof(s, xmlcontrol.tagfinal(), index, 1)
            If laststart <> npos AndAlso _
               lastend < laststart Then
                'if there is no tagleading before the index, or the tag finallized, do not need to reset the index
                index = laststart
            End If
            s = strleft(s, index) + Global.character.blank + ellipsis
#End If
    End Function
End Module
